-module(set_config).
-author("hpj").

-import(proplists, [get_value/2, get_value/3]).

-include("emqttd.hrl").

-include("mysql.hrl").

%% API
-export([handle/2]).

%% register user
handle(Ip,Params) ->
	Return = check(Ip,Params),
	case Return of 
		{ok,_} ->
			Return;
		{Record,UserInfo} ->
			Uid = get_value(<<"uid">>,Params),
			URL = binary_to_list(get_value(<<"url">>,Params,<<"">>)),
		  	case URL of
			  	"" ->
					{ok, [{<<"code">>,2},{<<"message">>,<<"url can't null">>}]};
			  	_  ->
			  		Token = binary_to_list(get_value(<<"token">>,Params)), 
				  	Timestamp = integer_to_list(tools:get_unixtime()),
				  	Echostr = tools:generate_rand(32),
				  	Signature = md5_string:md5_hex(tools:selection(Token++Timestamp++Echostr)),
					Request = lists:concat([URL,"?signature=",Signature,"&timestamp=",Timestamp,"&echostr=",Echostr]),
					case httpc:request(get, {Request, []}, [{timeout,?HTTP_TIMEOUT}], []) of
						{ok,{{_,ReturnCode,_},_,Body}} ->
							case ReturnCode =:= 200 andalso Body =:= Echostr of 
								true ->
									UserInfo1 = UserInfo#user_info{platform_type = 8,platform_special_flag = 1,ip = Ip},
									mnesia:dirty_write(user_info,UserInfo1),
									Record1 = Record#url_map{appkey = Uid,url = URL,num = Record#url_map.num + 1},
									Record2 = Record#url_map{appkey = Uid,url = URL,num = 0},
									mnesia:dirty_write(url_map,Record1),
									case Record#url_map.num of 
										0 ->
											timer:apply_after(timer:seconds(300),mnesia,dirty_write,[url_map,Record2]);
										_ ->
											ok
									end,		
									{ok,[{<<"code">>,0}]};
								_ ->
									{ok, [{<<"code">>,1},{<<"message">>,<<"url get request invalid">>}]}
							end;			
						_ ->
							{ok, [{<<"code">>,1}]}
					end			
		  	end
	end.		  	

%%业务校验流程
check( _Ip, Params) ->  
	Uid = get_value(<<"uid">>,Params),
  	Appkey = get_value(<<"appkey">>,Params),
  	SecretKey = get_value(<<"secretkey">>,Params),
	case mysql_utils:select(?IM_POOL,business,"secretkey",[{appkey,"=",Appkey,"and"},{secretkey,"=",SecretKey}]) of 
		[] ->
			{ok, [{<<"code">>,2},{<<"message">>,<<"appkey or secretkey error,please check it">>}]};
		_ ->
			case mnesia:dirty_match_object(user_info,#user_info{appkey = Appkey,id = Uid,_ ='_'}) of 
				[]	->
					{ok, [{<<"code">>,2},{<<"message">>,<<"uid not belong this appkey">>}]};
				[UserInfo] ->
					case UserInfo#user_info.status of 
		  				1 ->
		  					{ok, [{<<"code">>,7}]};
		  				_ ->
		  					case mnesia:dirty_read(url_map,Uid) of 
		  						[] ->
		  							{#url_map{num = 0},UserInfo};
		  						[Record] ->
%		  							case UserInfo#user_info.ip =:= undefined of 
%		  								true ->
%		  									case Record#url_map.num >= 300 of 
%		  										true ->
%		  											esockd:deny({http, 8184}, Ip),
%		  											timer:apply_after(tools:get_today_over_second(),mnesia,dirty_write,[url_map,Record#url_map{num = 0}]),
%		  											timer:apply_after(tools:get_today_over_second(),esockd,clear_access_rules,[{http, 8184}]),
%		  											{ok, [{<<"code">>,9}]};
%		  										false ->
		  											{ Record#url_map{ num = 0 },UserInfo }
%		  									end;		
%		  								false ->
%		  									case UserInfo#user_info.ip =:= Ip of 
%		  										true ->
%				  									case Record#url_map.num >= 300 of 
%				  										true ->
%				  											esockd:deny({http, 8184}, Ip),
%				  											timer:apply_after(tools:get_today_over_second(),mnesia,dirty_write,[url_map,Record#url_map{num = 0}]),
%				  											timer:apply_after(tools:get_today_over_second(),esockd,clear_access_rules,[{http, 8184}]),
%				  											{ok, [{<<"code">>,9}]};
%				  										false ->
%				  											{Record,UserInfo} 
%				  									end
%                                                    ;
%				  								false ->
%				  									{ok, [{<<"code">>,8}]}
%				  							end
%		  							end		
		  					end		
		  			end		
			end		
	end.			
